package com.hmo.automatic;

import android.app.Instrumentation;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.media.AudioManager;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import android.telecom.CallAudioState;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

import static android.content.Context.AUDIO_SERVICE;
import static android.content.Context.WINDOW_SERVICE;

public class PhoneCallReceiver extends BroadcastReceiver implements Runnable{
    private static final String TAG = "来电广播 ";
    private WindowManager windowManager;
    private Button button;
    private WindowManager.LayoutParams layoutParams;

    @Override
    public void onReceive(Context context, Intent intent) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);
        int state = tm.getCallState();
        Log.e(TAG, "state = "+state );
        switch (state){
            case TelephonyManager.CALL_STATE_RINGING:
                new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {

                    @Override
                    public void run() {

                        Intent intent1 = new Intent(context, MainActivity.class);
                        intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                        context.startActivity(intent1);
                        //MainActivity.setHook(true);
                        new Thread(PhoneCallReceiver.this).start();
                    }
                }, 2000);
                break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    AudioManager audioManager = (AudioManager)context.getSystemService(AUDIO_SERVICE);
                    try{
                        Log.e(TAG, "run: "+audioManager.getMode() );
                        audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
                        Log.e(TAG, "run: " +audioManager.isSpeakerphoneOn());
                        if(!audioManager.isSpeakerphoneOn()) {
                            audioManager.setSpeakerphoneOn(true);
                            Log.e(TAG, "run: "+audioManager.getMode() );
                            Log.e(TAG, "run: " +audioManager.isSpeakerphoneOn());
                            audioManager.setStreamVolume(
                                    AudioManager.STREAM_VOICE_CALL,
                                    audioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL),
                                    AudioManager.STREAM_VOICE_CALL
                            );
                        }
                    }catch (Exception e) {
                        e.printStackTrace();
                    }

                    break;
                case TelephonyManager.CALL_STATE_IDLE:
                    break;
        }
    }



    private void initFloatingWindow(Context context) {

        // 获取WindowManager服务
        windowManager = (WindowManager) context.getSystemService(WINDOW_SERVICE);

        button = new Button(context);
        button.setText("悬浮窗");
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(PhoneCallReceiver.this).start();
            }
        });
        // 设置LayoutParam
        layoutParams = new WindowManager.LayoutParams();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        }
        // 实现悬浮窗可以移动的属性（把这个值改成其他值可以操作悬浮窗底下的内容）
        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        layoutParams.format = PixelFormat.TRANSPARENT;
        layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.gravity = Gravity.CENTER;
        layoutParams.x = 0;
        layoutParams.y = 0;

        //todo
        //对你的悬浮窗做一些点击或移动事件的处理
    }

    @Override
    public void run() {
        SystemClock.sleep(3000);
        Instrumentation instrumentation = new Instrumentation();
        instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_HEADSETHOOK);
        Log.e(TAG, "run:接听结束" );
    }
}